package me.passtime.projt.account;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import me.passtime.projt.Main;
import me.passtime.projt.R;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.SystemUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 下午3:51
 */
public class RetrievePasswordStepTwoPage extends Activity implements View.OnClickListener {
    private EditText mMsgCode;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 5;
    private RetrievePasswordController mController;

    private String regId;
    private String code;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrieve_pwd_two);
        mMsgCode=(EditText)this.findViewById(R.id.retrieve_pwd_2_msg_code);

        Bundle extras = getIntent().getExtras();
        regId = extras.getString("regId");
        code = extras.getString("code");

        this.findViewById(R.id.retrieve_pwd_2_submit_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_2_send_btn).setOnClickListener(this);

        this.findViewById(R.id.retrieve_pwd_2_back_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_2_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_2_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_2_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_2_exit).setOnClickListener(this);

        mController=new RetrievePasswordController(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.retrieve_pwd_2_submit_btn:
                resetPassword();
                break;
            case R.id.retrieve_pwd_2_send_btn:
                sendMsgCodeAgain();
                break;
            case R.id.retrieve_pwd_2_back_btn:
                PageJumpUtils.jump(this, Main.class);
                break;
            case R.id.retrieve_pwd_2_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.retrieve_pwd_2_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.retrieve_pwd_2_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.retrieve_pwd_2_exit:
                AlertDialogUtils.showYesNoDiaLog(this,"是否退出系统?",new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }

    private void sendMsgCodeAgain() {
        showDialog(DIALOG_PROGRESS) ;
        mController.sendVerificationCode(regId,code);
    }

    private void resetPassword() {
        String msgCode=mMsgCode.getText().toString();
        if("".equals(msgCode)){
            AlertDialogUtils.showConfirmDiaLog(this,"请输入短信验证码");
            return;
        }
//        mController.resetPassword(regId,code,msgCode);
        Intent intent = new Intent(this, RetrievePasswordStepThreePage.class);
        intent.putExtra("regId", this.regId);
        intent.putExtra("code", this.code);
        intent.putExtra("msgCode", msgCode);
        this.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Main.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
